<?php require('./head.php');?>
<span style="font-size: 40px;font-weight: bold;display: block">3</span>
<a href="./list.php">立即返回模板列表</a>
<script>
    var num = 3;
    var t = setInterval(function () {
        num--;
        document.getElementsByTagName('span')[0].innerHTML = num;
        if(num <= 0) {
            window.location.href = './list.php';
            clearInterval(t);
        }
    },1000)
</script>
<?php
require('./db.php');
function func_response($str = '')
{
    echo $str;exit;
}
function func_request($str){
    return isset($_REQUEST[$str]) ? $_REQUEST[$str] : '';
}
$content = func_request("content");
$add = func_request("add");
$template = func_request("template");
$content = str_replace("'","''",$content);
if(!(is_numeric($template) && $template > 0)) {
    func_response('模板编号不正确！');
}
$sql = "SELECT * FROM `template` where k='{$template}' LIMIT 1";
$rs = Database::query($sql);
if ($add == 1) {
    if(!empty($rs)) {
        func_response('已存在，请换个模板编号error');
    }
    $sql = "INSERT INTO template(k,v,d)VALUES ('{$template}','{$content}',now());";
    if(Database::change($sql) > 0) {
        func_response('插入success');
    } else {
        func_response('插入error');
    }
}
if(empty($rs)) {
    func_response('该模板不存在error');
}
$sql = "UPDATE template set v='{$content}',d=now() where k='{$template}'";
//ob_clean();
//echo $sql;exit;
if(Database::change($sql) > 0) {
    func_response('修改success');
} else {
    func_response('修改error');
}
